(function(){
    //获取AccessToken 
    var accesstoken = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1, window.location.pathname.lastIndexOf('.'));
    //console.log(accesstoken);
    if (!accesstoken) {
        alert('无效的邀请');
        return;
    }

    window.main = function () { 
        var app = this; 
        app.checkSessionServiceName = "account.checksession";
        //app.loginServiceName = "";
        // 
        var session = window.sessionStorage;
       
        var token =  session.getItem('token');

        if (!token) {
            var url = window.location.protocol + '//' + window.location.host + '/invitation/preview/' + accesstoken + '.html';
            //console.log(url);
            self.location.href = url;
            return;
        }
        rsd.setAppToken(token);
        var jsHost = document.location.origin || '//agent.redmicro.cn/redjs/';
        if (document.location.host == "localhost") {
            jsHost = document.location.origin + "/redjs/"
            Rsd.__jsHomeHost = jsHost;
            Rsd.__isAgentHost = false;
        }
       
        setTimeout(function(){
             
            //推入一个新的state防止后退
            history.pushState({accessToken:accesstoken}, null, "#reload");
            history.pushState({}, null,  "#reload");
            //拦截后退操作 
            window.addEventListener('popstate', function(e) {
              
                var url = window.location.origin + '/invitation/preview/' + accesstoken + '.html';
                alert('popstate:'+url);
                //后退处理
                //if (e.state == null) {
                 //   window.location.href = url;
                //}
                  // 阻止默认后退行为
                  //e.preventDefault();
            });
            
        },1000);
        
        var onceCode = document.getElementById('onceCode').value;
        if (rsd.isEmpty(onceCode)) { 
           document.getElementById("buttons").remove();
        } 

    }
    /**
     * 
     */
    window.accept = function accept()
    {
        var onceCode = document.getElementById('onceCode').value;
        if (rsd.isEmpty(downloadService)) {
            alert('非法请求');
            return;
        }
        var acceptServiceName = document.getElementById("acceptServiceName").value;
        if(rsd.isEmpty(acceptServiceName)) 
        {
            alert('接口服务不存在');
            return;
        }

        $app.http(acceptServiceName,{
            accessToken:accesstoken,
            onceCode:onceCode
        })
        .then(function(reps){
            window.location.href=window.location.href;
        })
        .cacth(function(reps){
            alert(resp.msg);
        }); 
    },
    /**
     * 
     */
    window.refuse = function refuse()
    {
        var onceCode = document.getElementById('onceCode').value;
        if (rsd.isEmpty(downloadService)) {
            alert('非法请求');
            return;
        }
        var refuseServiceName = document.getElementById("refuseServiceName").value;
        if(rsd.isEmpty(refuseServiceName)) 
        {
            alert('接口服务不存在');
            return;
        }

        $app.http(refuseServiceName,{
            accessToken:accesstoken,
            onceCode:onceCode
        })
        .then(function(reps){
            window.location.href=window.location.href;
        })
        .cacth(function(reps){
            alert(resp.msg);
        }); 
    }

})()